#include"queue.h"


void QueueInit(Queue* q)
{
	assert(q);

	q->front = NULL;
	q->rear = NULL;
	q->size = 0;

}


void QueuePush(Queue* q, QueDataType data)
{
	assert(q);
	QueListNode* newnode = (QueListNode*)malloc(sizeof(QueListNode));
	if (newnode == NULL)
	{
		perror("malloc fail\n");
		return;
	}
	newnode->data = data;
	newnode->next = NULL;
	if (q->front == NULL)
	{
		q->front = q->rear = newnode;
	}
	else
	{
		q->rear->next = newnode;
		q->rear =newnode;
	}
	q->size++;
}


int QueueEmpty(Queue* q)
{
	assert(q);
	return !q->size;
}

void QueuePop(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));


	if (q->front->next == NULL)
	{
		free(q->front);
		q->front = q->rear = NULL;
	}
	else
	{
		QueListNode* next = q->front->next;
		free(q->front);
		q->front = next;

	}
	q->size--;
}

QueDataType QueueFront(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));

	return q->front->data;
}

QueDataType QueueBack(Queue* q)
{
	assert(q);
	assert(！QueueEmpty(q));

	return q->rear->data;
}

int QueueSize(Queue* q)
{
	assert(q);
	return q->size;
}

void QueueDestroy(Queue* q)
{
	assert(q);
	while (q->front)
	{
		QueListNode* next = q->front->next;
		free(q->front);
		q->front = next;
	}
	q->front = NULL;
	q->rear = NULL;
	q->size = 0;
}